const {sequelize} = require('../../core/db')
const { Sequelize, Model, Op } = require('sequelize');
const {
    Art
} = require('../models/art')


class Favor extends Model {
    static async like(art_id, type, uid) {
       const favor =await Favor.findOne({
           where: {
            art_id,
            type,
            uid
           }
       });
       if(favor) {
          throw new global.errs.LikeError();
       }
       
       return sequelize.transaction(async t => {
           await Favor.create({
            art_id,
            type,
            uid
           }, {transaction: t});
           const art = await Art.getData(art_id, type);
           await art.increment('fav_nums',{
               by:1,
               transaction: t
            })
       })
       
    }

    static async disLike(art_id, type, uid) {
        const favor = await Favor.findOne({
            where: {
                art_id,
                type,
                uid
            }
        })
        if (!favor) {
            throw new global.errs.DislikeError()
        }
        // Favor 表 favor 记录
        return sequelize.transaction(async t => {
            await favor.destroy({
                force: true,
                transaction: t
            })
            const art = await Art.getData(art_id, type)
            await art.decrement('fav_nums', {
                by: 1,
                transaction: t
            })
        })
    }

    // 该用户是否点赞
    static async userLikeIt(art_id, type, uid) {
        const favor = await Favor.findOne({
            where: {
                art_id,
                type,
                uid
            }
        });
        return favor ? true: false;
    }
    

    static async getMyClassicFavor(uid) {
        const arts = await Favor.findAll({
            where: {
               uid,
               type: {
                   [Op.not]: 400
               } 
            }
        });
        if(!arts) {
           throw new global.errs.NotFound();
        }
        return await Art.getList(arts);

    }

}

Favor.init({
    uid: Sequelize.INTEGER,
    art_id: Sequelize.INTEGER,
    type: Sequelize.INTEGER
},{
    sequelize,
    tableName: 'favor'
})

module.exports = {
    Favor
}